package com.trkj.zzplus.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;
import com.trkj.zzplus.R;
import com.trkj.zzplus.ui.fragment.MainUiFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Linoer on 2016/6/5.
 */
public class UIDemo extends AppCompatActivity {


    private ViewPager mViewPager;//ViewPager
    private TextView mNewsType;  //新闻类型
    private TextView mNewsTypeEn;   //新闻类型English
    private TextView mTab;      //顶部指示器
    private ImageView mMore;    //更多
    private ImageView mUser;    //用户
    private ResideMenu mResideMenu;     //侧边栏
    private ResideMenuItem mResideMenuItem;
    private FrameLayout ignored_view;     //防止侧滑冲突
    private FragmentPagerAdapter mAdapter;

    //新闻类型数组
    private String[] titles = new String[]{
            "热门", "要闻", "时尚娱乐", "株洲时政",
            "株洲民生", "军事历史", "微话题", "爆料", "专题"
    };
    private String[] titlesEn = new String[]{
            "The Hot", "Focus",
            "Fashion Circle", "Politics",
            "Livelihood", "History",
            "Micro Topic", "Broke",
            "Special"
    };

    private Fragment[] mFragments = new MainUiFragment[titles.length];
    private List<Fragment> mViews = new ArrayList<Fragment>();

    //侧边栏监听事件
    private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
        @Override
        public void openMenu() {

        }

        @Override
        public void closeMenu() {

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_demo_main);
        //初始化组件
        initView();

        //初始化事件
        initEvent();

        //默认显示首页、首标题
        setSelect(0);

        mNewsType.setText(titles[0]);
    }


    private void initEvent() {

        //ViewPager监听器
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int currentItem = mViewPager.getCurrentItem();
                mNewsType.setText(titles[position]);
                mNewsTypeEn.setText(titlesEn[position]);
                setTab(currentItem);
               /* Bundle bundle = new Bundle();
                bundle.putString("newsType",titles[position]);
                mFragments[position].setArguments(bundle);*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //按钮适配器
        mMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mResideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
            }
        });
        mUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mResideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
            }
        });

    }


    //导航栏显示卡
    private void setTab(int i) {
        switch (i) {
            case 0:
                mTab.setText("● ○ ○ ○ ○ ○ ○ ○ ○");
                break;
            case 1:
                mTab.setText("○ ● ○ ○ ○ ○ ○ ○ ○");
                break;
            case 2:
                mTab.setText("○ ○ ● ○ ○ ○ ○ ○ ○");
                break;
            case 3:
                mTab.setText("○ ○ ○ ● ○ ○ ○ ○ ○");
                break;
            case 4:
                mTab.setText("○ ○ ○ ○ ● ○ ○ ○ ○");
                break;
            case 5:
                mTab.setText("○ ○ ○ ○ ○ ● ○ ○ ○");
                break;
            case 6:
                mTab.setText("○ ○ ○ ○ ○ ○ ● ○ ○");
                break;
            case 7:
                mTab.setText("○ ○ ○ ○ ○ ○ ○ ● ○");
                break;
            case 8:
                mTab.setText("○ ○ ○ ○ ○ ○ ○ ○ ●");
                break;
            default:
                break;
        }
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.ui_demo_viewpager);
        mTab = (TextView) findViewById(R.id.ui_demo_tab);
        mNewsType = (TextView) findViewById(R.id.ui_newsType);
        mNewsTypeEn = (TextView) findViewById(R.id.ui_newsType_English);

        mMore = (ImageView) findViewById(R.id.ui_more);
        mUser = (ImageView) findViewById(R.id.ui_user);

        ignored_view = (FrameLayout) findViewById(R.id.ui_ignore_fragment);

        //初始化侧边栏
        initReSideMenu();

        //初始化适配器
        initAdapter();
    }

    private void initAdapter() {
        for (int i = 0; i < titles.length; i++) {
            mFragments[i] = new MainUiFragment();
            mViews.add(mFragments[i]);
        }
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mViews.get(position);
            }

            @Override
            public int getCount() {
                return mViews.size();
            }
        };
        mViewPager.setAdapter(mAdapter);
    }

    private void initReSideMenu() {
        mResideMenu = new ResideMenu(this);
        mResideMenu.setBackground(R.mipmap.ui_mybg);

        //设置是否有阴影
        mResideMenu.setShadowVisible(false);


        mResideMenu.attachToActivity(this);
        mResideMenu.setMenuListener(menuListener);
        mResideMenu.setScaleValue(0.5f);

        //取消对组件的滑动焦点，释放给包裹在FrameLayout中的ListView
        mResideMenu.addIgnoredView(ignored_view);

        //侧边栏图片标题
        int icon[] = {R.mipmap.qq_about_unpressed, R.mipmap.qq_dynamic, R.mipmap.qq_dynamic, R.mipmap.qq_dynamic};
        String titles[] = {"Home", "Profile", "Calendar", "Settings"};

        for (int i = 0; i < titles.length; i++) {
            mResideMenuItem = new ResideMenuItem(this, icon[i], titles[i]);

            //左边栏,可同时添加右边栏
            mResideMenu.addMenuItem(mResideMenuItem, ResideMenu.DIRECTION_LEFT);
        }
        for (int i = 0; i < titles.length; i++) {
            mResideMenuItem = new ResideMenuItem(this, icon[i], titles[i]);

            //左边栏,可同时添加右边栏
            mResideMenu.addMenuItem(mResideMenuItem, ResideMenu.DIRECTION_RIGHT);
        }
    }

    //选中当前页面
    private void setSelect(int i) {
        setTab(i);
        mViewPager.setCurrentItem(i);
    }

}
